
Page({
  data: {
    itrahost:"https://itra.run",
    pointpaths:['/pages/images/p_0.png','/pages/images/p_1.png','/pages/images/p_2.png','/pages/images/p_3.png','/pages/images/p_4.png','/pages/images/p_5.png','/pages/images/p_6.png',],
    racename:'',
    showModal:false,
    year:"",
    session:'',
    application:'',
    isLoaded:false,
    hadData:false,
    list:[]
  },
  
  bindDateChange: function(e) {  
    this.setData({  
      year: e.detail.value,
      showModal:true
    })  
  },
  onNameInputChange(e) {
    let inputName = e.detail.value
    this.setData({
      racename: inputName
    })
  },
  onLoad:function(){
    this.runnerinfo();
  },
  runnerinfo:function(e){
    var that = this;
    const app = getApp();
    wx.request({
      url: app.globalData.itra_info,
      method:"GET",
      success:(res)=>{
        var response = res.data;
        console.log(response);
        var session = response.Session;
        var application = response.Application;
        that.setData({
          session:session,
          application:application,
        })
      },
      fail: (err) => {
        wx.showModal({
          title: '温馨提示',
          content: "网络较慢,数据请求失败!",
          showCancel:false,
          confirmText:"确定",
          confirmColor:"#000000"
        })
      }
    })
},

  loadData:function(){
    if(this.data.racename == '' || this.data.racename==null ){
      wx.showToast({
        title: '请输入赛事名称拼音',
        icon:'error'
      });
      return;
    }
    const app = getApp();
    if(this.data.year==''|| this.data.year==null){
      wx.showToast({
        title: '请选择年份',
        icon:'error'
      });
      return;
    }
      wx.showLoading({
        title: '获取数据中...',
        mask:true
      })
      var that = this;
      wx.request({
        url: app.globalData.runnerResultUrl,
        data:{
          "Year":this.data.year,
          "start":"0",
          "length":"100",
          "SearchTerms":this.data.racename
        },
        header:{
          "Accept-Encoding": "gzip, deflate, br",
          "Accept": "*/*",
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
          "Connection": "keep-alive",
          'Cookie': '.AspNetCore.Session='+this.data.session+'; .AspNetCore.Identity.Application='+this.data.application
        },
        method:"POST",
        success:(res)=>{
          var results = res.data;
          console.log(results);
          that.setData({
            list:results.data,
            isLoaded:true,
            hadData:results.data.length>0
          })
          wx.hideLoading();
        },
        fail: (err) => {//请求失败
          wx.hideLoading();
          that.setData({
            isLoaded:true,
            hadData:false
          })
        }
      })
  },
  onItem:function(e){
    var index = e.currentTarget.id;
      var obj = this.data.list[index];
      var info = {
        'itraPoints':obj.itraPoints,
        'raceYearId':obj.raceYearId,
      };
      var str = encodeURIComponent(JSON.stringify(info));
      wx.navigateTo({
        url: '../races/races?str='+str
      })
  },
})
